package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.mysql.UserWalletDao;
import com.juyu.miliao.api.domain.CoinPrice;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.UserWallet;
import com.juyu.miliao.api.domain.Vip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * H5支付用户信息service
 *
 * @author xuanqicheng
 * @create 2018-12-10:38 AM
 */
@Service
public class H5PayDetailService {

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private UserWalletDao userWalletDao;

    public Object getUserInfoById(long userId){
        JSONObject resultJson = new JSONObject();
        try {
            User user = userInfoDao.getUserInfoById(userId);

            UserWallet userWallet =userWalletDao.getUserWalletById(userId);

            if (user == null) {
                resultJson.put("code", 300);
                resultJson.put("message", "用户不存在");
                return resultJson;
            } else {

                resultJson.put("code", 200);
                resultJson.put("message", "成功");
                JSONObject data = new JSONObject();
                data.put("userId", user.getUserId());
                data.put("icon", user.getIcon());
                data.put("nickName", user.getNickName());
                data.put("isVip",user.getIsVip());
                data.put("isSvip",user.getIsSvip());
                resultJson.put("data", data);
                JSONObject wallet = new JSONObject();
                wallet.put("deposit",userWallet.getDeposit());
                wallet.put("diamond",userWallet.getDiamond());
                resultJson.put("wallet",wallet);
                return resultJson;
            }
        }catch (Exception ex){
            resultJson.put("code", 300);
            resultJson.put("message", "用户不存在");
            return resultJson;
        }

    }

    /**
     * 获取金币展示列表
     * @param list
     * @return
     */
    public List<Map<String,Object>> getGoldList(List<CoinPrice> list) throws  Exception{

        List<Map<String,Object>> coinList = new ArrayList<>(0);

        for(CoinPrice coinPrice: list){

            Map<String,Object> map = new HashMap<>(0);

            int contentId = coinPrice.getCoinId();

            map.put("contentId",contentId);

            String[] strs = coinPrice.getShowPrice().split("#");

            String showPrice = strs[0];

            map.put("showPrice",showPrice);

            String showDiscount = "";

            if(strs.length==2){

                showDiscount = strs[1];
            }

            map.put("showDiscount",showDiscount);

            String showCoin = coinPrice.getShowCoin();

            map.put("showDesc",showCoin);
            coinList.add(map);
        }

        return coinList;
    }

    public List<Map<String, Object>> getVipList(List<Vip> list)throws  Exception {
        List<Map<String,Object>> vipList = new ArrayList<>(0);
        for(Vip vip : list ){
            Map<String,Object> map = new HashMap<>(0);
            map.put("contentId",vip.getVipId());
            map.put("price",vip.getPrice());
            map.put("showPrice",vip.getShowPrice());
            map.put("showDesc",vip.getShowDesc());
            map.put("validTime",vip.getValidTime());
            map.put("vipType",vip.getVipType());
            map.put("showDailyPrice",vip.getShowDailyPrice());
            map.put("vipDiscount",vip.getVipDiscount());
            map.put("showOriginalPrice",vip.getShowOriginalPrice());
            vipList.add(map);
        }
        return vipList;
    }
}
